// 3D盒子动画效果
function init3DBox() {
    const container = document.getElementById('3d-box-container');
    if (!container) return;

    // 初始化场景、相机和渲染器
    const width = container.clientWidth;
    const height = container.clientHeight;
    
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
    renderer.setSize(width, height);
    renderer.setClearColor(0x000000, 0); // 透明背景
    container.appendChild(renderer.domElement);
    
    // 创建金属质感的3D盒子
    // 创建圆角立方体 - 使用RoundedBoxGeometry
    const radius = 0.2; // 圆角半径
    const segments = 5; // 圆角分段数
    const geometry = new THREE.RoundedBoxGeometry(2.5, 2.5, 2.5, segments, radius); // 放大立方体尺寸
    
    // 移除原来的顶点修改代码
    const position = geometry.attributes.position;
    const vertex = new THREE.Vector3();
    
    for (let i = 0; i < position.count; i++) {
        vertex.fromBufferAttribute(position, i);
        
        // 计算顶点到立方体中心的距离
        const absX = Math.abs(vertex.x);
        const absY = Math.abs(vertex.y);
        const absZ = Math.abs(vertex.z);
        
        // 只处理角上的顶点
        if (absX > 0.9 && absY > 0.9 && absZ > 0.9) {
            const direction = vertex.clone().normalize();
            const length = Math.sqrt(vertex.x*vertex.x + vertex.y*vertex.y + vertex.z*vertex.z);
            
            // 计算圆角位置
            const factor = (length - radius) / length;
            vertex.multiplyScalar(factor);
            
            position.setXYZ(i, vertex.x, vertex.y, vertex.z);
        }
    }
    position.needsUpdate = true;
    geometry.computeVertexNormals(); // 重新计算法线
    // 创建Canvas生成AI浮雕纹理
    const canvas = document.createElement('canvas');
    const size = 512;
    canvas.width = size;
    canvas.height = size;
    const ctx = canvas.getContext('2d');
    
    // 绘制背景
    ctx.fillStyle = '#001A33'; // 更深的蓝黑色背景
    ctx.fillRect(0, 0, size, size);
    
    // 绘制AI文字
    ctx.fillStyle = '#FFFFFF'; // 使用白色提高对比度
    ctx.font = 'Bold 200px Arial'; // 增大AI文字大小
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    ctx.fillText('AI', size/2, size/2);
    
    // 移除发光效果
    ctx.shadowColor = 'transparent';
    ctx.shadowBlur = 0;
    
    // 创建法线贴图
    const aiTexture = new THREE.CanvasTexture(canvas);
    aiTexture.minFilter = THREE.LinearFilter;
    
    // 深蓝黑色金属材质
    const material = new THREE.MeshPhysicalMaterial({
        color: 0x001A33, // 深蓝黑色立方体
        metalness: 0.9,
        roughness: 0.2,
        clearcoat: 1.0,
        clearcoatRoughness: 0.05,
        envMapIntensity: 2.0,
        normalMap: aiTexture, // 添加生成的AI法线贴图
        normalScale: new THREE.Vector2(1.5, 1.5), // 增强浮雕效果
        emissive: 0x000000, // 移除发光效果
        emissiveIntensity: 0.0 // 设置为0
    });
    const cube = new THREE.Mesh(geometry, material);
    scene.add(cube);
    
    // 添加灯光
    const ambientLight = new THREE.AmbientLight(0xffffff, 2); // 增强环境光
    scene.add(ambientLight);
    
    const directionalLight1 = new THREE.DirectionalLight(0xffffff, 50);
    directionalLight1.position.set(1.5, 1.5, 1);
    scene.add(directionalLight1);
    
    const directionalLight2 = new THREE.DirectionalLight(0xffffff, 10);
    directionalLight2.position.set(-1.5, -1.5, -1);
    scene.add(directionalLight2);
    
    // 移除点光源
    // const pointLight = new THREE.PointLight(0xffffff, 2, 10);
    // pointLight.position.set(0, 0, 3);
    // scene.add(pointLight);
    
    camera.position.z = 5;
    
    // 添加动画
    function animate() {
        requestAnimationFrame(animate);
        
        // 缓慢旋转
        cube.rotation.x += 0.005;
        cube.rotation.y += 0.01;
        
        renderer.render(scene, camera);
    }
    
    animate();
    
    // 响应式调整
    window.addEventListener('resize', function() {
        const width = container.clientWidth;
        const height = container.clientHeight;
        camera.aspect = width / height;
        camera.updateProjectionMatrix();
        renderer.setSize(width, height);
    });
}

// 当DOM加载完成后初始化3D盒子
document.addEventListener('DOMContentLoaded', function() {
    init3DBox();
});